import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout } from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'
import { NO_RESET_WHITE_LIST } from '@/constants'

const { t } = useI18n()

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/dispatch',
    name: 'Root',
    meta: {
      hidden: true
    }
  },
  {
    path: '/redirect',
    component: Layout,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login'),
      noTagsView: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/dispatch',
    component: Layout,
    redirect: '/dispatch/taskpool',
    name: 'Dispatch',
    meta: {
      title: t('dispatch.dispatch'),
      icon: 'material-symbols:schedule-send',
      alwaysShow: true
    },
    children: [
      {
        path: 'rule',
        component: () => import('@/views/Dispatch/Rule/Rule.vue'),
        name: 'DispatchRule',
        meta: {
          title: t('dispatch.rule'),
          icon: 'material-symbols:settings-cinematic-blur-outline'
        }
      },
      {
        path: 'taskpool',
        component: () => import('@/views/Dispatch/TaskPool/Pool.vue'),
        name: 'DispatchPool',
        meta: {
          title: t('dispatch.taskpool'),
          icon: 'grommet-icons:task'
        }
      },
      {
        path: 'needplan',
        component: () => import('@/views/Dispatch/NeedPlan/Plan.vue'),
        name: 'DispatchSupplement',
        meta: {
          title: t('dispatch.needplan'),
          icon: 'material-symbols:next-plan-outline'
        }
      },
      {
        path: 'need',
        component: () => import('@/views/Dispatch/Need/Need.vue'),
        name: 'DispatchNeed',
        meta: {
          title: t('dispatch.need'),
          icon: 'tabler:list'
        }
      },
      {
        path: 'team',
        component: () => import('@/views/Dispatch/Team/Team.vue'),
        name: 'DispatchTeam',
        meta: {
          title: t('dispatch.team'),
          icon: 'oui:ml-outlier-detection-job'
        }
      },
      // {
      //   path: 'feedback',
      //   component: () => import('@/views/Dispatch/Feedback/Feedback.vue'),
      //   name: 'DispatchFeedback',
      //   meta: {
      //     title: t('dispatch.feedback'),
      //     icon: 'fluent-mdl2:survey-questions'
      //   }
      // },
      // {
      //   path: 'assignment',
      //   component: () => import('@/views/Dispatch/Assignment/Assignment.vue'),
      //   name: 'DispatchAssignment',
      //   meta: {
      //     title: t('dispatch.assignment'),
      //     icon: 'iwwa:assign'
      //   }
      // },
      {
        path: 'feedback2',
        component: () => import('@/views/Dispatch/Feedback2/Feedback.vue'),
        name: 'DispatchFeedback2',
        meta: {
          title: t('dispatch.feedback'),
          icon: 'fluent-mdl2:survey-questions'
        }
      },
      {
        path: 'assignment2',
        component: () => import('@/views/Dispatch/Assignment2/Assignment.vue'),
        name: 'DispatchAssignment2',
        meta: {
          title: t('dispatch.assignment'),
          icon: 'iwwa:assign'
        }
      }
    ]
  },
  {
    path: '/check',
    component: Layout,
    redirect: '/check/vendor',
    name: 'Check',
    meta: {
      title: t('check.check'),
      icon: 'material-symbols-light:high-quality',
      alwaysShow: true
    },
    children: [
      {
        path: 'vendor',
        component: () => import('@/views/Check/Vendor/Vendor.vue'),
        name: 'CheckVendor',
        meta: {
          title: t('check.vendor'),
          icon: 'material-symbols:manage-accounts-outline'
        }
      },
      {
        path: 'vendor/:vendor',
        component: () => import('@/views/Check/Vendor/Detail.vue'),
        name: 'CheckVendorDetail',
        meta: {
          title: t('check.vendorEvaluate'),
          noTagsView: true,
          noCache: true,
          hidden: true,
          showMainRoute: true,
          activeMenu: '/check/vendor'
        }
      },
      {
        path: 'params',
        component: () => import('@/views/Check/Params/Params.vue'),
        name: 'CheckParams',
        meta: {
          title: t('check.params'),
          icon: 'material-symbols:manufacturing'
        }
      },
      {
        path: 'sample',
        component: () => import('@/views/Check/Sample/Sample.vue'),
        name: 'CheckSample',
        meta: {
          title: t('check.sample'),
          icon: 'ph:standard-definition'
        }
      },
      {
        path: 'feature',
        component: () => import('@/views/Check/Feature/Feature.vue'),
        name: 'CheckASRFeature',
        meta: {
          title: t('check.feature'),
          icon: 'mdi:feature-search-outline'
        }
      },
      {
        path: 'female',
        component: () => import('@/views/Check/QARFemale/QARFemale.vue'),
        name: 'CheckQARFemale',
        meta: {
          title: t('check.female'),
          icon: 'carbon:parent-child'
        }
      },
      {
        path: 'female-add/:id?',
        component: () => import('@/views/Check/QARFemale/Add.vue'),
        name: 'CheckQARFemaleAdd',
        meta: {
          title: t('check.femaleEdit'),
          noTagsView: true,
          noCache: true,
          hidden: true,
          showMainRoute: true,
          activeMenu: '/check/female'
        }
      },
      {
        path: 'qar',
        component: () => import('@/views/Check/QARTask/QARTask.vue'),
        name: 'CheckQARTask',
        meta: {
          title: t('check.qar'),
          icon: 'simple-icons:testcafe'
        }
      },
      {
        path: 'qar-add/:id?',
        component: () => import('@/views/Check/QAR/Add.vue'),
        name: 'CheckQARFormAdd',
        meta: {
          title: t('check.qarEdit'),
          noTagsView: true,
          noCache: true,
          hidden: true,
          showMainRoute: true,
          activeMenu: '/check/qar'
        }
      },
      {
        path: 'asr',
        component: () => import('@/views/Check/ASR/ASR.vue'),
        name: 'CheckASRForm',
        meta: {
          title: t('check.asr'),
          icon: 'hugeicons:investigation'
        }
      },
      {
        path: 'asr-add/:id?',
        component: () => import('@/views/Check/ASR/Add.vue'),
        name: 'CheckASRFormAdd',
        meta: {
          title: t('check.asrEdit'),
          noTagsView: true,
          noCache: true,
          hidden: true,
          showMainRoute: true,
          activeMenu: '/check/asr'
        }
      },
      {
        path: 'order',
        component: () => import('@/views/Check/Order/Order.vue'),
        name: 'CheckOrderForm',
        meta: {
          title: t('check.order'),
          icon: 'solar:test-tube-outline'
        }
      },
      {
        path: 'order-add/:id?',
        component: () => import('@/views/Check/Order/Add.vue'),
        name: 'CheckOrderFormAdd',
        meta: {
          title: t('check.orderEdit'),
          noTagsView: true,
          noCache: true,
          hidden: true,
          showMainRoute: true,
          activeMenu: '/check/order'
        }
      },
      {
        path: 'plan',
        component: () => import('@/views/Check/Plan/Plan.vue'),
        name: 'CheckQARPlan',
        meta: {
          title: t('check.plan'),
          icon: 'material-symbols:next-plan-outline'
        }
      },
      {
        path: 'monitor',
        component: () => import('@/views/Record/Record.vue'),
        name: 'Record',
        meta: {
          title: t('check.monitor'),
          icon: 'ep:data-analysis'
        }
      }
    ]
  },
  {
    path: '/doc',
    component: Layout,
    redirect: '/docs',
    name: 'Doc',
    meta: {
      title: t('用户文档'),
      icon: 'material-symbols:book-outline',
      hidden: true
    },
    children: [
      {
        path: '/docs',
        component: () => import('@/views/Doc/Doc.vue'),
        name: 'Docs',
        meta: {
          title: t('操作手册'),
          icon: 'material-symbols:book-outline'
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !NO_RESET_WHITE_LIST.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
